package com.jrzh.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * SecurityUser继承于实体对象User，并实现了Spring Security的UserDetails，同时重载了getAuthorities方法
 * 用来取得为用户分配的角色列表，用于后面的权限验证
 * @author Xanthin
 *
 */
public class SecurityUser implements UserDetails{
	private static final long serialVersionUID = 1L;
	
	private Long id;
	private String name;
	private String password;
	private List<String> roles;
	
	public SecurityUser(Long id,String name,String password,List<String> roles) {
		this.id = id;
		this.name = name;
		this.password = password;
		this.roles = roles;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		List<String> roles = this.getRoles();
		if(roles != null){
			for(String role : roles){
				SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
				authorities.add(authority);
			}
		}
		return authorities;
	}

	@Override
	public String getPassword() {
		return password;
	}
	
	@Override
	public String getUsername() {
		return name;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
	
	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public List<String> getRoles() {
		return roles;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRoles(List<String> roles) {
		this.roles = roles;
	}

	@Override
	public String toString() {
		return "id【"+this.getId()+"】"+"name【"+this.getName()+"】"+"password【"+this.getPassword()+"】";
	}

}
